Skip to content

fix: The directory /run/lastore does not exist.#334

Merged
qiuzhiqian merged 1 commit intomasterfrom
fix-dir-not-found
Mar 20, 2026
Merged

fix: The directory /run/lastore does not exist.#334
qiuzhiqian merged 1 commit intomasterfrom
fix-dir-not-found

Conversation

@qiuzhiqian
Copy link

No description provided.

@github-actions
Copy link

github-actions bot commented Mar 20, 2026

CLA Assistant Lite bot:
提交邮箱中包含我们的合作伙伴,但您似乎并非合作伙伴的成员或对接人,请联系相关对接人将您添加至组织之中,或由其重新发起 Pull Request。
The commit email domain belongs to one of our partners, but it seems you are not yet a member of the current organization, please contact the contact person to add you to the organization or let them submit the Pull Request.

xml seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: qiuzhiqian, zhaohuiw42

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码片段展示了在 Go 语言中处理日志文件时的一个逻辑片段,主要涉及文件删除、目录创建和文件重新打开。以下是对这段代码的审查意见,包括语法逻辑、代码质量、性能和安全方面的改进建议:

1. 语法逻辑审查

问题:目录创建时机与文件删除的冲突

_ = os.RemoveAll(logTmpPath)  // 删除文件或目录
// ...
if err := os.MkdirAll(filepath.Dir(logTmpPath), 0755); err != nil { // 重新创建目录
    return fmt.Errorf("failed to mkdir %s: %v", filepath.Dir(logTmpPath), err)
}
m.logTmpFile, err = os.OpenFile(logTmpPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) // 重新创建文件
  • 逻辑分析:代码先执行 os.RemoveAll 删除了 logTmpPath(可能是文件或目录),紧接着又尝试创建其父目录。
  • 潜在风险:如果 logTmpPath 指向的是一个文件,RemoveAll 会删除该文件,此时 filepath.Dir(logTmpPath) 获取的父目录通常是已经存在的。如果父目录不存在(极罕见情况),MkdirAll 是有意义的;但如果父目录一直存在,这行代码就是冗余的。
  • 建议:明确 logTmpPath 的语义。如果它始终是一个文件路径,通常不需要每次都 MkdirAll,除非父目录可能被外部进程删除。如果它可能是目录,那么 RemoveAll 后的 MkdirAll 是合理的。

改进建议

  • 如果 logTmpPath 是固定路径且父目录不会变动,可以移除 MkdirAll,或者将其移到循环/函数外部(如果这段代码在循环中)。
  • 如果必须确保目录存在,可以添加注释说明原因:
    // Ensure parent directory exists (it might be deleted by external processes)
    if err := os.MkdirAll(filepath.Dir(logTmpPath), 0755); err != nil {
        return fmt.Errorf("failed to mkdir %s: %v", filepath.Dir(logTmpPath), err)
    }

2. 代码质量审查

问题 1:错误处理不一致

_ = os.RemoveAll(logTmpPath)
_ = m.logTmpFile.Close()
  • 使用 _ 忽略了 RemoveAllClose 的错误。虽然这在某些场景下是可以接受的(例如清理资源时忽略错误),但最好记录日志或至少添加注释说明为什么忽略错误。

改进建议

if err := os.RemoveAll(logTmpPath); err != nil {
    log.Warningf("failed to remove %s: %v", logTmpPath, err) // 假设有日志库
}
if m.logTmpFile != nil {
    if err := m.logTmpFile.Close(); err != nil {
        log.Warningf("failed to close log file: %v", err)
    }
}

问题 2:变量 err 的来源

  • m.logTmpFile, err = os.OpenFile(...) 中的 err 是从哪里来的?如果这是函数内部的一个局部变量,建议显式声明:
    var err error
    m.logTmpFile, err = os.OpenFile(...)

3. 代码性能审查

  • os.RemoveAllos.MkdirAll 是系统调用,频繁调用可能会影响性能。如果这段代码在循环或高频路径中,建议优化:
    • 检查是否真的需要每次都删除和重建文件/目录。
    • 可以考虑使用文件锁或原子操作来避免竞争条件。

4. 代码安全审查

问题 1:权限设置

os.MkdirAll(filepath.Dir(logTmpPath), 0755) // 目录权限 0755
os.OpenFile(logTmpPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) // 文件权限 0644
  • 07550644 是常见的权限设置,但需要确保这些权限符合安全策略。例如,日志文件是否允许其他用户读取?
  • 如果日志文件包含敏感信息,建议使用更严格的权限(如 0600)。

问题 2:路径遍历风险

  • logTmpPath 是否是可控的?如果它来自用户输入,可能会导致路径遍历攻击(如 ../../../etc/passwd)。建议对路径进行校验:
    if !filepath.IsAbs(logTmpPath) {
        return fmt.Errorf("log path must be absolute: %s", logTmpPath)
    }

5. 其他建议

代码可读性

  • 可以将文件清理和重建的逻辑封装成一个单独的函数,提高可读性和复用性:
    func (m *Manager) recreateLogFile(logPath string) error {
        if err := os.RemoveAll(logPath); err != nil {
            log.Warningf("failed to remove %s: %v", logPath, err)
        }
        if m.logTmpFile != nil {
            if err := m.logTmpFile.Close(); err != nil {
                log.Warningf("failed to close log file: %v", err)
            }
            m.logTmpFile = nil
        }
        if err := os.MkdirAll(filepath.Dir(logPath), 0755); err != nil {
            return fmt.Errorf("failed to mkdir %s: %v", filepath.Dir(logPath), err)
        }
        var err error
        m.logTmpFile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
        if err != nil {
            return fmt.Errorf("failed to open file %s: %v", logPath, err)
        }
        return nil
    }

总结

  1. 逻辑:确认 MkdirAll 的必要性,避免冗余操作。
  2. 质量:改进错误处理,明确变量声明。
  3. 性能:避免高频系统调用,优化文件操作。
  4. 安全:检查权限设置和路径安全性。

改进后的代码会更健壮、可读性更强,同时减少潜在的安全和性能问题。

@qiuzhiqian qiuzhiqian merged commit e460004 into master Mar 20, 2026
24 of 29 checks passed
@qiuzhiqian qiuzhiqian deleted the fix-dir-not-found branch March 20, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants